$(function(){
  	createTree();
  	initMenu();
  	$("#addline").linkbutton({
  	  iconCls: 'icon-search'  
  	});
});
var p_name = "系统菜单";
var p_mid = "000";
var p_mcode = "000";
var p_depth = 0;
/**
 * 创建菜单树
 */
function createTree(){
   $("#tree").tree({
      url:'menuContorller/queryNavigationMenu.do?action=tree&tid=-1',
      onBeforeExpand:function(node,param){      	 
         $('#tree').tree('options').url = "menuContorller/queryNavigationMenu.do?action=tree&tid="+node.id;
      },
      lines : true,
      onClick:function(node){      	  
      	  p_name = node.text;
      	  p_mid = node.id;
      	  p_mcode = node.code;
      	  p_depth = node.depth;
      	  var jsons = {"Id":node.id}; 
      	  reloadTable({tableId:'menus',params:jsons});//刷新列表
      }
   });
}
/**
 * 初始化菜单列表
 */
function initMenu(){
	dataTable({
      tableId:'menus',
      title:'菜单列表',
      url:'menuContorller/queryMenu.do',
      idField:'mid',
      columns:[[
		 {field : 'mid',checkbox : true,align : 'center'},
		 {title : '菜单名称',field : 'mname',width : 150},
		 {title : '菜单编码',field : 'mcode',width : 150},
		 {title : '菜单地址',field : 'murl',width : 300,formatter:function(value,row,index){
		    return '<span style="font-size:16px;">'+value+'</span>';
		 }},
		 {title : '是否可用',field : 'mstate',width:100,formatter:function(value,row,index){
		    if(value=='0'){
		       return '<span style="color:red;">不可用</span>';
		    }else{
		      return '<span style="color:blue;">可用</span>';
		    }
		 }}
		 ]],
	  showPage:true,
	  toolbar:'toolbar'
   });
}
$("#menu_add").click(function(){
	var url = "pages/sys/menu/addMenu.jsp";	
	url = urlAddParmert(url,"p_name",p_name);
	url = urlAddParmert(url,"p_mid",p_mid);
	url = urlAddParmert(url,"p_mcode",p_mcode);
	url = urlAddParmert(url,"p_depth",p_depth);	
    easyuiWidow({
   	 title:'添加菜单',
     winId:'addWin',
     width:800,   
     height:400,
     iconCls:'icon-win-addmenu',
     url:url
   });
});
//修改数据
$("#menu_update").click(function(){
   var rows = $("#menus").datagrid("getChecked");
   var len = rows.length;
   if(len<1){
   	  $.messager.alert("系统提示", "请选择要修改的数据！","warning");
   	  return;
   }else if(len>1){
   	  $.messager.alert("系统提示", "每次只能修改一条数据，请重新选择！","warning");
   	  return;
   }
   updateRowData(rows[0].mid);   
});
//删除数据
$("#menu_delete").click(function(){  
   var rows = $('#menus').datagrid('getSelections');
   var len = rows.length;
   if(len==0){
      $.messager.alert("系统提示","请选择要删除的记录","warning");
      return;
   }
   var ids = new Array();
   for(var i = 0;i<len;i++){     
   	  ids.push(rows[i].mid);
   }
   deleteRowData(ids);
});
function updateRowData(menuId){  
   var url = "menuContorller.do?method=findMenu";
   url = urlAddParmert(url,"menuId",menuId);
   url = urlAddParmert(url,"pname",p_name);	
   //打开easyui窗口
   easyuiWidow({
   	 title:'修改菜单',
     winId:'updateWin',
     width:800,   
     height:400,
     iconCls:'icon-win-addmenu',
     url:url
   });
}
//删除数据
function deleteRowData(menuIds){   
   var len = 1;
   var ids = new Array();
   if(typeof(menuIds)!='string'){
     len = menuIds.length;
     ids = menuIds;
   }else{
     ids.push(menuIds);
   }
   var url = "menuContorller.do?method=deleteMenu";
    //删除数据
   deleteData({
     len:len,
     url:url,
     params:{'id':ids,'version':Math.random()},
     dataType:'text',
     callback:function(msg){
        if(msg=="EMPTY"){
           $.messager.alert("系统提示","请选择要删除的记录","warning");
        }else if(msg=="FAIL"){
           $.messager.alert("系统提示","删除菜单信息失败","warning");
        }else if(msg=="SUCCESS"){
           reloadTable({tableId:'menus'});
		   reloadTree({treeId:'tree'}); 
        }else{
            $.messager.alert("系统提示","系统出现异常："+msg,"error");
        }
     }
   });
}
function viewDetail(menuId){
   var url = "menuContorller.do?method=viewDetailMenu";
   url = urlAddParmert(url,"menuId",menuId);
   url = urlAddParmert(url,"pname",p_name);	
   //打开easyui窗口
   easyuiWidow({
   	 title:'查看菜单详细',
     winId:'updateWin',
     width:800,   
     height:400,
     iconCls:'icon-win-addmenu',
     url:url
   });
}
function saveRowData(){
   alert('44444');
}